using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BlackCubeMod : MonoBehaviour
{
    public List<GameObject> TowerList;
    public List<GameObject> TrapsList;
    public List<GameObject> BlackDoorList;
    public List<Transform> RoomsList;
    public float amountFade = 1;
    private bool containsTower = false;
    private bool containsTraps = false;
    private bool containsBlackDoor = false;
    public bool executeFadeOutLast = true;
    public bool DontTriggerStay = false;
    private float timer;
    public float endTime = 15;
    public float FadeOutAmount = 1;
    public float FadeInAmount = 0;
    private float FadeOutGameObject = 1;
    private float FadeInGameObject = 0;
    private int second;
    private int minutes;
    public static BlackCubeMod getInstance;

    void Awake()
    {
        getInstance = this;
    }

    void Start()
    {
        TowerList = new List<GameObject>();
        TrapsList = new List<GameObject>();
        BlackDoorList = new List<GameObject>();
        RoomsList = new List<Transform>();

        foreach (GameObject gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
        {
            if (gameObj.name == "Tower")
            {
                TowerList.Add(gameObj);
                containsTower = true;
            }

            if (gameObj.name == "Door")
            {
                if (gameObj.renderer.material.color == Color.black)
                {
                    containsBlackDoor = true;
                    BlackDoorList.Add(gameObj);
                }

            }

            if (gameObj.tag == "Traps")
            {
                if (!gameObj.name.Equals("Ball"))
                {
                    containsTraps = true;
                    TrapsList.Add(gameObj);
                }

            }
        }


        foreach (Transform gameObj in FindObjectsOfType(typeof(Transform)) as Transform[])
        {


            if (gameObj.name == "_Rooms")
            {
                RoomsList.Add(gameObj);
                executeFadeOutLast = true;
            }
        }
    }

    void Update()
    {

        if (!DontTriggerStay)
        {
            Timer();
        }

        DesactiveAllGameObjects();
        FadeAllFloor();

        if (second > endTime)
        {
            FadeOutGameObject += (amountFade * Time.deltaTime) / 2;
        }

    }

    void DesactiveAllGameObjects()
    {

        if (containsTower)
        {

            foreach (GameObject gameObj in TowerList)
            {

                gameObj.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                gameObj.renderer.material.color = new Color(gameObj.renderer.material.color.r, gameObj.renderer.material.color.g,
                gameObj.renderer.material.color.b, FadeOutGameObject);
                gameObj.GetComponent<Tower>().StopAllCoroutines();


            }

            foreach (GameObject gameObj in TowerList)
            {
                foreach (Transform gbm in gameObj.transform)
                {
                    if (gbm.name == "Front")
                    {
                        gbm.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                        gbm.renderer.material.color = new Color(gbm.renderer.material.color.r, gbm.renderer.material.color.g,
                        gbm.renderer.material.color.b, FadeOutGameObject);
                    }
                }
            }


            if (second > endTime)
            {

                foreach (GameObject gameObj in TowerList)
                {
                    foreach (Transform gbm in gameObj.transform)
                    {
                        if (gbm.name == "Front")
                        {
                            gbm.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                            gbm.renderer.material.color = new Color(gbm.renderer.material.color.r, gbm.renderer.material.color.g,
                            gbm.renderer.material.color.b, FadeInGameObject);
                        }
                    }
                }

                foreach (GameObject gameObj in TowerList)
                {
                    gameObj.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                    gameObj.renderer.material.color = new Color(gameObj.renderer.material.color.r, gameObj.renderer.material.color.g,
                    gameObj.renderer.material.color.b, FadeInGameObject);
                    gameObj.GetComponent<Tower>().StartCoroutine("fire");

                }

                containsTower = false;

            }


        }

        if (containsTraps)
        {



            foreach (GameObject gameObj in TrapsList)
            {

                gameObj.GetComponent<MovePlataform>().enabled = false;
                gameObj.collider.enabled = false;
                gameObj.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                gameObj.renderer.material.color = new Color(gameObj.renderer.material.color.r, gameObj.renderer.material.color.g,
                gameObj.renderer.material.color.b, FadeOutGameObject);

            }

            if (second > endTime)
            {


                foreach (GameObject gameObj in TrapsList)
                {

                    gameObj.GetComponent<MovePlataform>().enabled = true;
                    gameObj.collider.enabled = true;
                    gameObj.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                    gameObj.renderer.material.color = new Color(gameObj.renderer.material.color.r, gameObj.renderer.material.color.g,
                    gameObj.renderer.material.color.b, FadeInGameObject);

                }

                containsTraps = false;
            }
        }

        if (containsBlackDoor)
        {

            foreach (GameObject gameObj in BlackDoorList)
            {

                gameObj.collider.isTrigger = true;
                gameObj.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                gameObj.renderer.material.color = new Color(gameObj.renderer.material.color.r, gameObj.renderer.material.color.g,
                gameObj.renderer.material.color.b, FadeOutGameObject);
            }


            if (second > endTime)
            {

                foreach (GameObject gameObj in BlackDoorList)
                {

                    gameObj.collider.isTrigger = false;
                    gameObj.renderer.material.color = new Color(gameObj.renderer.material.color.r, gameObj.renderer.material.color.g,
                    gameObj.renderer.material.color.b, FadeInGameObject);


                }

                containsBlackDoor = false;
            }
        }

        if (FadeOutGameObject >= 0.1f)
        {

            FadeOutGameObject -= (amountFade * Time.deltaTime) / 2;

        }

        if (second > endTime - 2)
        {

            FadeInGameObject += (amountFade * Time.deltaTime);

        }
    }

    public void Timer()
    {
        timer += Time.deltaTime;

        if (timer >= 1)
        {

            if (second < 59)
            {
                second++ ;

            }
            else
            {

                second = 0;
                minutes++;

            }

            timer = 0;
        }

    }

    public void FadeAllFloor()
    {

        if (executeFadeOutLast)
        {

            foreach (GameObject gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
            {

                if (gameObj.name == "_FadeDoors")
                {

                    gameObj.SetActive(false);

                }

            }
            if (FadeInAmount >= 0.1f)
            {



            }


            FadeInAmount += (amountFade * Time.deltaTime);



            foreach (Transform t in RoomsList)
            {

                foreach (Transform a in t)
                {

                    foreach (Transform s in a)
                    {
                        if (a != CheckpointManager.getInstance.currentRoom)
                        {
                            if (s.name == "Floor")
                            {

                                s.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                                s.renderer.material.color = new Color(s.renderer.material.color.r, s.renderer.material.color.g,
                        s.renderer.material.color.b, FadeInAmount);
                            }
                        }

                    }
                }
            }

            if (second > endTime)
            {

                FadeOutAmount -= (amountFade * Time.deltaTime) / 2;

                foreach (Transform t in RoomsList)
                {

                    foreach (Transform a in t)
                    {
                        if (a != CheckpointManager.getInstance.currentRoom)
                        {
                            foreach (Transform s in a)
                            {

                                if (s.name == "Floor")
                                {

                                    s.renderer.material.shader = Shader.Find("Transparent/Diffuse");
                                    s.renderer.material.color = new Color(s.renderer.material.color.r, s.renderer.material.color.g,
            s.renderer.material.color.b, FadeOutAmount);


                                }
                            }
                        }
                    }
                }
            }



            if (FadeInAmount >= 1 && FadeOutAmount <= 0)
            {
                executeFadeOutLast = false;
            }
        }

    }
}
